//
//  SampuxRecordView.m
//  Sampux
//
//  Created by rzm on 16/7/12.
//  Copyright © 2016年 rzm. All rights reserved.
//

#import "SampuxRecordView.h"
#import "SampuxRecordViewCell.h"

@interface SampuxRecordView ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, copy) NSString *modeStr;

@end

@implementation SampuxRecordView

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
//        [self getData];
        _dataArr = [NSMutableArray array];
        [self setupSubView];
    }
    return self;
}
//
//- (void)getData{
//    
//    NSArray *arrStr = @[@"定时设置",@"温度设置",@"模式设置"];
//    NSString *tempStr = @"定时设置";
////    NSString *tempStr = @"温度设置";
////    NSString *tempStr = @"模式设置";
//    _modeStr = tempStr;
//    NSInteger index = arc4random()%2;
//    self.modeStr = arrStr[index];
//}

- (void)setupSubView
{
    _tbView = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, kScreenWidth, kScreenHeight - 64)];
    [self addSubview:_tbView];
    _tbView.dataSource = self;
    _tbView.delegate = self;
    _tbView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tbView.backgroundColor = THSystemClearColor;

    _header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(refreshDataWithHeader)];
    _tbView.mj_header = _header;
    [_tbView.mj_header beginRefreshing];
    _footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(refreshDataWithfooter)];
    _tbView.mj_footer = _footer;
}

- (void)setUserCount:(NSString *)userCount
{
    _userCount = userCount;
    
}

- (void)refreshDataWithHeader
{
    [self.dataArr removeAllObjects];
    [GBInterMaster_CommForDeivce requestUserOpes:0 withTag:0 withDeviceAccount:self.model.deviceAccount withUserAccount:self.userCount];
    _tbView.mj_header.automaticallyChangeAlpha = YES;
}

- (void)refreshDataWithfooter
{
    [GBInterMaster_CommForDeivce requestUserOpes:0 withTag:1 withDeviceAccount:self.model.deviceAccount withUserAccount:self.userCount];
    [_footer endRefreshing];
}

- (void)setDataArr:(NSMutableArray *)dataArr
{
    dispatch_async(dispatch_get_main_queue(), ^{
        _dataArr = dataArr;
        [_tbView reloadData];
        [_header endRefreshing];
    });
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    SampuxRecordViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (cell == nil) {
        cell = [[SampuxRecordViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    if (self.dataArr.count != 0) {
        SPUserOperationModel *model = self.dataArr[indexPath.row];
        cell.model = model;
        cell.nubLab.text = [NSString stringWithFormat:@"%d",indexPath.row + 1];
//        [self getData];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.backgroundColor = THSystemClearColor;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return Sampux6HScale(100);
}
@end
